let messagePort=null;

function factorial(n){
    try{
        let result=1;
        while(n){ result*=n--;}
        return BigInt(result).toString();
    }catch(e){
        return "无穷大";
    }
}
//在全局对象上添加消息处理程序
self.onmessage=({ports})=>{
    console.log('工作者线程',ports);
    
    if(!messagePort){
        messagePort=ports[0];
        self.onmessage=null;
        messagePort.onmessage=({data})=>{
            messagePort.postMessage(`${data}!=${factorial(data)}`);
        }
    }
}